package cn.wolfcode.business.controller;

import cn.wolfcode.business.domain.BusBpmnInfo;
import cn.wolfcode.business.service.BusBpmnInfoService;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpResponse;
import java.sql.Statement;
import java.util.List;

/**
 * 审核流程定义
 */
@RestController
@RequestMapping("/business/bpmnInfo")
public class BusBpmnInfoController extends BaseController {

    @Autowired
    BusBpmnInfoService busBpmnInfoService;


    /**
     * 获取流程定义列表中的所有流程定义及其相关信息
     */
    @GetMapping("/list")
    public TableDataInfo getBpmnInfo() {
        startPage();
        List<BusBpmnInfo> bpmnInfoList = busBpmnInfoService.getBpmnInfoList();
        return getDataTable(bpmnInfoList);
    }

    /**
     * 部署流程定义
     * 需要从前端接收接收三个参数 流程定义的类型   流程文件（bpmn文件）  以及描述信息
     *
     * @return
     */
    @PostMapping
    public AjaxResult add(Integer bpmnType, MultipartFile file, String info) throws IOException {
        return AjaxResult.success(busBpmnInfoService.add(bpmnType, file, info));
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable  Long id){
        return AjaxResult.success(busBpmnInfoService.delete(id));
    }

    /**
     * 获取部署的流程定义的bpmn文件或者是png图片
     */
    @GetMapping("/readResource/{id}/{type}")
    public void readResource(@PathVariable Long id , @PathVariable String type, HttpServletResponse response) throws IOException {
        InputStream inputStream = busBpmnInfoService.readResource(id, type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }
}
